﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.OleDb;
using Aandelendelen.Db;
using Aandelendelen.Mail;
using Aandelendelen.Obs;
using Aandelendelen.Ad;

namespace Aandelendelen.Controls
{
	public partial class AdvertiserAddCampaignControl : AuthenticationRequiredAdvertiserBaseUserControl
	{
		protected override void OnFirstHit(object sender, EventArgs e)
		{
			this.Panel1.Visible = true;
			this.Panel2.Visible = false;
			this.Panel3.Visible = false;
		}

		protected override void OnPostBack(object sender, EventArgs e)
		{
			if (Request.Form[this.btnChange.UniqueID] != null)
			{
				this.Panel1.Visible = true;
				this.Panel2.Visible = false;
				this.Panel3.Visible = false;

				return;
			}

			if (Request.Form[this.btnAkkoord.UniqueID] != null)
			{
				int campaignId = this.RegisterAd();

				this.SendPaymentInfoMail(campaignId);

				this.Panel1.Visible = false;
				this.Panel2.Visible = false;
				this.Panel3.Visible = true;

				return;
			}

			Page.Validate();

			if (Page.IsValid)
			{
				this.Panel1.Visible = false;
				this.Panel2.Visible = true;
				this.Panel3.Visible = false;

				this.SetControls();
			}

		}

		private void SetControls()
		{
			this.hprBanner.ImageUrl = this.tbBannerUrl.Text;
			this.hprBanner.Text = this.tbTooltip.Text;
			this.hprBanner.NavigateUrl = this.tbTargetUrl.Text;

			int number = Convert.ToInt32(this.tbNumber.Text);
			int numberTimes1000 = number * 1000;
			this.lblViews.Text = numberTimes1000.ToString();

			this.lblCost.Amount = this.Cost;
		}

		private int RegisterAd()
		{
			int newId = -1;

			QueryArguments args = new QueryArguments();

			args.Add("UserId", this.UserInfo.Id, OleDbType.Integer);
			args.Add("BannerUrl", this.tbBannerUrl.Text, OleDbType.Char);
			args.Add("TargetUrl", this.tbTargetUrl.Text, OleDbType.Char);
			args.Add("Tooltip", this.tbTooltip.Text, OleDbType.Char);
			args.Add("MaxViews", Convert.ToInt32(this.tbNumber.Text) * 1000, OleDbType.Integer);
			args.Add("Cost", this.Cost, OleDbType.Currency);

			Query query = new Query("i_adnewad", args);
			newId = (int)query.Execute();

			return newId;
		}

		private void SendPaymentInfoMail(int campaignId)
		{
			MailArguments mailArgs = MailArguments.MailArgs;
			mailArgs.Clear();

			int aantal = Convert.ToInt32(this.tbNumber.Text) * 1000;

			mailArgs["{aanhef}"] = this.UserInfo.MailAanhef;
			mailArgs["{banner}"] = this.tbBannerUrl.Text;
			mailArgs["{url}"] = this.tbTargetUrl.Text;
			mailArgs["{tooltip}"] = this.tbTooltip.Text;
			mailArgs["{aantal}"] = aantal.ToString();
			mailArgs["{bedragbankexbtw}"] = ObjectFormatter.GetCurrencyString(this.Cost);
			mailArgs["{bedragpayalexbtw}"] = ObjectFormatter.GetCurrencyString(Calculator.GetPayPalPrice(this.Cost));
			mailArgs["{uid}"] = this.UserInfo.Gebruikersnaam;
			mailArgs["{id}"] = campaignId.ToString();

			EMail email = MailCache.GetMail("adregistered");
			email.Recipients = this.UserInfo.Email;

			email.QueueSingle();
		}

		private decimal Cost
		{
			get
			{
				int number = Convert.ToInt32(this.tbNumber.Text);

				return AdvertiseCache.GetAdvertiseOptions().GetBannerViewsCost(number);
			}
		}

		protected void CustomValidator1_ServerValidate(object source, ServerValidateEventArgs args)
		{
			//validate emailadres
			int number;

			try
			{
				args.IsValid = true;

				number = Convert.ToInt32(args.Value);

				if (number < 1 || number > 9999)
				{
					args.IsValid = false;

					this.CustomValidator1.Text = "Aantal moet tussen 1 en 9999 liggen";
				}

			}
			catch
			{
				this.CustomValidator1.Text = "Geen correct aantal";

				args.IsValid = false;
			}
		}
	}
}